// $Id: PerspectiveMenu.java 25 2011-09-18 19:44:05Z huijzer@gmail.com $
package lime.application.menu;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class PerspectiveMenu extends ContributionItem {

	private static final String KEY_PERSPECTIVE_DESCRIPTOR = "key_perspective_descriptor";

	private final SelectionListener menuListener = new PerspectiveMenuListener();

	private static IWorkbenchPage getActivePage() {
		IWorkbenchPage result = null;
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			result = window.getActivePage();
		}
		return result;
	}

	private static String getPerspectiveId() {
		String result = null;
		IWorkbenchPage page = getActivePage();
		if (page != null) {
			IPerspectiveDescriptor descriptor = page.getPerspective();
			if (descriptor != null) {
				result = descriptor.getId();
			}
		}
		return result;
	}

	@Override
	public final boolean isDynamic() {
		return true;
	}

	/**
	 * Fills a drop-down menu with all available perspectives. The current one
	 * is selected.
	 */
	@Override
	public void fill(Menu menu, int index) {
		String activePerspective = getPerspectiveId();

		IPerspectiveDescriptor[] perspectives = PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();

		for (int i = 0; i < perspectives.length; i++) {
			IPerspectiveDescriptor descriptor = perspectives[i];

			MenuItem item = new MenuItem(menu, SWT.RADIO, i);
			item.setData(KEY_PERSPECTIVE_DESCRIPTOR, descriptor);
			item.setText(descriptor.getLabel());

			final Image image = descriptor.getImageDescriptor().createImage();
			item.setImage(image);

			item.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					image.dispose();
				}
			});

			item.addSelectionListener(menuListener);

			if (descriptor.getId().equals(activePerspective)) {
				item.setSelection(true);
			}
		}
	}

	/**
	 * Switch perspective in the active page
	 */
	private static final class PerspectiveMenuListener extends SelectionAdapter {
		public void widgetSelected(SelectionEvent e) {
			MenuItem item = (MenuItem) e.widget;
			if (item.getSelection()) {
				IWorkbenchPage page = getActivePage();
				if (page != null) {
					IPerspectiveDescriptor descriptor = (IPerspectiveDescriptor) item
							.getData(KEY_PERSPECTIVE_DESCRIPTOR);
					page.setPerspective(descriptor);
				}
			}
		}
	}

}